from datetime import datetime

from qturboframe.lib.utils.by import By
from qturboframe.lib.utils.CustomError.custom_error import CustomError
from qturboframe.lib.utils.assertion import Assertion
from qturboframe.lib.ranorex.client import CsClient
from qturboframe.lib.utils.controlname import ControlName
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.win_control.text import Text as WinText
from qturboframe.lib.ranorex.win_control.tree import Tree as WinTree
from qturboframe.lib.ranorex.win_control.tree_item import TreeItem
from .checkbox import CheckBox

from qturboframe.public_ui.ucf_wpf.basecontrol import BaseControl
from qturboframe.public_ui.ucf_wpf.button import Button


class Tree(BaseControl):
    """
    wpf ucf树形控件

    :Attributes:
     - _driver: 控件所在的当前Control对象
     - _control: 控件元素Control对象
     - tree: 当前的树形控件对象
     - timeout: 步骤操作的超时时间

     :Raises
        CsClientError 没有找到控件异常

    :Example:
     - 例1: Tree({By.XPATH:"/DateTime"})
     - 例2: Tree({By.AUTOMATIONID:'automationid'})
     - 例3: Tree(control)
     - 例4: Tree({By.TEXT: 'Click'}, father=client)
     - 例5: Tree({By.AUTOMATIONID:'automationid'}, father=client)
     - 例6: Tree(control, father=client)
    """

    def __init__(self, element_para, father=None, timeout=10000):
        super().__init__(element_para, father, timeout)
        self.tree = self._control

        if self.tree.get_control_name()==ControlName.TREE:
            self.tree = WinTree(self._control.control)
        else:
            raise CsClientError(CustomError.Error_EQ03001000_Cannot_Find_Element,"根据“{}”获取的控件不是树形控件".format(element_para))

    def select(self, node_text):
        '''
        选择树形控件上的节点

        :Args:
         - node_text - 树形控件上节点的文字例如：0/0.0, 1/1.0/1.0.1

        :Example:
         - 例: tree.select("0/0.0, 1/1.0/1.0.1")
        '''
        #取消所有已做的选择
        self.__deselect_all()
        node_list = node_text.split(",")

        for node in node_list:
            first_node = self.tree
            node_text_list = node.split("/")
            last_node_text_list = node_text_list[1:]
            print(last_node_text_list)
            for node_text_temp in node_text_list:
                xpath = ".//text[@text='{}']".format(node_text_temp.strip())
                node_control = first_node.find_element_by_attribute(By.XPATH, xpath, timeout=1000)
                xpath = ".//ancestor::treeitem"
                tree_item_control = node_control.find_element_by_attribute(By.XPATH, xpath, timeout=1000)
                tree_item = TreeItem(tree_item_control.control)
                if len(last_node_text_list)>0:
                    if not tree_item.is_expanded():
                        Button({By.XPATH:".//button"}, tree_item_control, timeout=1000).click()
                else:
                    CheckBox({By.XPATH:".//checkbox"}, tree_item_control, timeout=1000).checked_all_checkboxes()
                first_node = tree_item_control
                last_node_text_list = last_node_text_list[1:]

    def __deselect_all(self):
        #获取到树形控件的一级节点
        xpath = ".//container[@automationid='TreeViewScrollViewer']/container/container/treeitem"
        first_tree_nodes = self.tree.find_elements_by_attribute(By.XPATH, xpath, timeout=self.timeout)

        for node in first_tree_nodes:
            CheckBox({By.XPATH:".//checkbox"}, node, timeout=1000).unchecked_all_checkboxes()


